<?php
declare (strict_types=1);

namespace app\admin\controller\base;

use app\admin\model\base\SysConfig as SysConfigModel;
use app\admin\validates\SysConfigValidate;
use app\BaseController;
use think\exception\ValidateException;
use think\facade\Request;
use think\response\Json;
use upload\ImgUpload;

/**
 * 系统配置中心
 */
class SysConfig extends BaseController
{
    /**
     * 系统配置图片上传
     * @return Json
     */
    public function imageUpload(): Json
    {
        $folder = 'sys_config';
        $file = request()->file('picture');
        if (!$file) {
            return jsonReturn(0, []);
        }
        //处理上传图片信息
        $image = ImgUpload::imgUploadAliyun($file, $folder, 0, 0, 1000);
        return jsonReturn(1, $image, '上传成功');
    }

    /**
     * 系统配置图片上传
     * @return Json
     */
    public function imageUploads(): Json
    {
        $folder = 'sys_config';
        $file = request()->file('picture');
        if (!$file) {
            return jsonReturn(0, []);
        }
        //处理上传图片信息
        $image = ImgUpload::imgUpload($file, $folder, 0, 0, 1000);
        return jsonReturn(1, $image, '上传成功');
    }

    /**
     * 系统配置列表
     * @return Json
     */
    public function list(): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $data = SysConfigModel::getList([], $params['page'], $params['limit']);
        return jsonReturn(1, $data);
    }

    /**
     * 系统配置编辑
     * @return Json
     */
    public function edit(): Json
    {
        $params = Request::param();
        //数据验证
        try {
            validate(SysConfigValidate::class)->scene($params['key'])->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //根据不同配置做参数校验
        switch ($params['key']) {
            case 'WsOpenPlatform'://开放平台服务商
                $value = [
                    'name' => $params['name'],
                    'appid' => $params['appid'],
                    'secret' => $params['secret'],
                    'aes_key' => $params['aes_key'],
                    'token' => $params['token'],
                ];
                break;
            case 'WsOfficialAccount'://兜底公众号
                $value = [
                    'name' => $params['name'],
                    'appid' => $params['appid'],
                    'secret' => $params['secret'],
                    'qrcode' => $params['qrcode'],
                    'token' => $params['token'],
                ];
                break;
            case 'WsScanBag'://取袋回调配置
                $value = [
                    'douyin_client_key' => $params['douyin_client_key'],
                    'douyin_redirect_uri' => $params['douyin_redirect_uri'],
                    'v1_iot_pay_uri' => $params['v1_iot_pay_uri'],
                ];
                break;
            case 'DataCenter'://大屏数据优化
                $value = [
                    'data_center_start' => $params['data_center_start'],
                    'data_center_out_bag_num' => $params['data_center_out_bag_num'],
                ];
                break;
            default:
                return jsonReturn(0, [], 'Key参数错误');
        }
        //组装数据
        $params['value'] = json_encode($value);
        $params['update_time'] = time();
        //写入数据库
        $Model = SysConfigModel::where(['key' => $params['key']])->find();
        if (!$Model) {
            $params['create_time'] = time();
            $Model = new SysConfigModel();
        }
        $res = $Model->save($params);
        if ($res) {
            //清除缓存
            SysConfigModel::cleanUpConfig();
            return jsonReturn(1, [], '修改成功');
        }
        return jsonReturn(0, [], '修改失败');
    }


}
